package com.wwj.springboot.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.wwj.springboot.entity.CmfzBanner;
import com.wwj.springboot.service.CmfzBannerService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author wwj
 */
@Controller
@RequestMapping("banner")
public class BannerController {

    @Value("myfile-path")
    private String getPath;

    @Autowired
    private CmfzBannerService cbs;
    @Value("myfile")
    private String path;

    @RequestMapping("selectByPage")
    @ResponseBody
    public Map selectByPage(int page ,int limit){
        return cbs.selectByPage(page, limit);
    }

    @RequestMapping("selectOne")
    @ResponseBody
    public CmfzBanner selectOne(int id){
        return cbs.selectOne(id);
    }
    @RequestMapping("deleteBanner")
    @ResponseBody
    public Map deleteBanner(int id){
        return cbs.deleteBanner(id);
    }



    @RequestMapping("addBanner")
    @ResponseBody
    public Map addBanner(CmfzBanner banner){
        return cbs.addBanner(banner);
    }

    @RequestMapping("updateBanner")
    @ResponseBody
    public Map updateBanner(CmfzBanner banner){
        return cbs.updateBanner(banner);
    }




    @RequestMapping("downloadList.xls")
    public void downloadList(HttpServletResponse response, HttpSession session){
        List<CmfzBanner> cmfzBanners =  cbs.selectAll();
        for (CmfzBanner cmfzGuru : cmfzBanners) {
            cmfzGuru.setBannerImageUrl("E:/IDEA/Idea_Work/myFile/"+cmfzGuru.getBannerImageUrl());
        }
        ExportParams exportParams = new ExportParams("标题","CmfzBanner");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CmfzBanner.class,cmfzBanners);

        try {

            response.setHeader("Content-Disposition",
                    "attachment;fileName="+ URLEncoder.encode(
                            "轮播图.xls","utf-8"
                    ));
            response.setContentType("application/vnd.ms-excel");
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("uploadList")
    @ResponseBody
    public Map uploadList(MultipartFile file) throws Exception {

        InputStream inputStream = file.getInputStream();
        /**
         * setTitleRows(1) 设置标题所占的行数
         * setHeadRows(1) 设置表头所占的行数
         */
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setTitleRows(1);
        /**
         * 参数1 流
         * 参数2 pojoClass 普通Java对象的类对象 要导入的实体类的类对象
         * 参数3 导入参数对象
         */
        List<CmfzBanner> cmfzBanners = ExcelImportUtil.importExcel(inputStream, CmfzBanner.class, importParams);

        for (CmfzBanner cmfz : cmfzBanners){
            String s = StringUtils.substringAfterLast(cmfz.getBannerImageUrl(), "\\");
            cmfz.setBannerImageUrl(s);
        }

        return cbs.uploadList(cmfzBanners);
    }



}
